﻿package component.tree.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import component.tree.dao.ConnectionProvider;

public class ConfigUtil {
	public static String context = "/tree";//从其它地方注入该值
	
	public static String COMPONENT_CONFIG_FILE = "/component.properties";
	
	public static Map configMap;
	
	static{
		configMap = new HashMap();
	
		Properties pros = new Properties();
		try {
			pros.load(ConnectionProvider.class.getResourceAsStream(COMPONENT_CONFIG_FILE));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Iterator iter = pros.entrySet().iterator();
		while(iter.hasNext()){
			Entry e = (Entry) iter.next();
			String key = (String)e.getKey();
			String value = (String)e.getValue();
			configMap.put(key, value);
		}
	}
	
	public static String getConnectionProvider(){
		return (String)configMap.get("connectionProvider");
	}
	
	public static String getTreeModel(String id){
		return (String)configMap.get(id);
	}
	
	/**
	 * 获取树组件资源的相对路径
	 * @return
	 */
	public static String getTreeDir(){
		return (String)configMap.get("treeDir");
	}
	
	public static String getDbType(){
		return (String)configMap.get("dbType");
	}
	
	/**
	 * 获取树型需要的js
	 * @return
	 */
	public static String getImportTreeJs(){
		StringBuffer sb = new StringBuffer(100);
		
		String jsFile = (String)configMap.get("jsFile");
		String[] jsArr = jsFile.split(";");
		for(int i = 0;i<jsArr.length;i++){
			String temp = jsArr[i];
			temp = temp.replaceAll("<context>", context);
			temp = temp.replaceAll("<treeDir>", getTreeDir());
			sb.append("<script src='").append(temp).append("'></script> \n");
		}
		
		return sb.toString();
	}
	
	/**
	 * 获取树型需要的css
	 * @return
	 */
	public static String getImportTreeCss(){
		StringBuffer sb = new StringBuffer(100);
		
		String cssFile = (String)configMap.get("cssFile");
		String[] cssArr = cssFile.split(";");
		for(int i = 0;i<cssArr.length;i++){
			String temp = cssArr[i];
			temp = temp.replaceAll("<context>", context);
			temp = temp.replaceAll("<treeDir>", getTreeDir());
			sb.append("<link rel='StyleSheet' href='").append(temp).append("' type='text/css' /> \n");
		}
		
		return sb.toString();
	}
	
	/*************树型样式***************/
	public static String getTreeCssName(){
		return (String)configMap.get("treeCssName");
	}
	public static String getTreeNodeCssName(){
		return (String)configMap.get("treeNodeCssName");
	}
	public static String getNodeCssName(){
		return (String)configMap.get("nodeCssName");
	}
	public static String getParentNodeCssName(){
		return (String)configMap.get("parentNodeCssName");
	}
	/*************树型样式***************/
	
	
	
	/*************树型图片***************/
	public static String getRootImageName(){
		return context + getTreeDir() + "/images/" + (String)configMap.get("rootImageName");
	}
	public static String getPlusImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("plusImageName");
	}
	public static String getMinusImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("minusImageName");
	}
	public static String getNolinesPlusImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("nolinesPlusImageName");
	}
	public static String getNolinesMinusImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("nolinesMinusImageName");
	}
	public static String getPlusbottomImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("plusbottomImageName");
	}
	public static String getMinusbottomImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("minusbottomImageName");
	}
	public static String getFolderImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("folderImageName");
	}
	public static String getFolderopenImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("folderopenImageName");
	}
	public static String getPageImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("pageImageName");
	}
	public static String getJoinImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("joinImageName");
	}
	public static String getJoinbottomImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("joinbottomImageName");
	}
	public static String getLineImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("lineImageName");
	}
	public static String getEmptyImageName(){
		return context + getTreeDir() + "/images/"  + (String)configMap.get("emptyImageName");
	}
	
	
	
	/*************树型图片***************/
	
	public static void main(String[] args) {

	}
	
	private boolean isMatche(String str,String filterStr){
		filterStr = filterStr.replace('.', '#');
		filterStr = filterStr.replaceAll("#", "\\\\.");
		filterStr = filterStr.replace('*', '#');
		filterStr = filterStr.replaceAll("#", ".*");
		filterStr = filterStr.replace('?', '#');
		filterStr = filterStr.replaceAll("#", ".?");
		filterStr = "^" + filterStr + "$";

		Pattern p = Pattern.compile(filterStr);
		Matcher fMatcher = p.matcher(str);
		return fMatcher.matches();
	}
}
